package org.ponly.cta4j.factory.math;

/**
 * 抽象数学运算符.
 *
 * @author vacoor
 * @since 1.0
 */
public abstract class AbstractMathOperation implements MathOperation {
    /**
     * 加法运算.
     */
    public static final MathOperation PLUS = new AbstractMathOperation("+") {
        /**
         * {@inheritDoc}
         */
        @Override
        public int eval(final int a, final int b) {
            return a + b;
        }
    };

    /**
     * 减法运算.
     */
    public static final MathOperation MINUS = new AbstractMathOperation("-") {
        /**
         * {@inheritDoc}
         */
        @Override
        public int eval(final int a, final int b) {
            return a - b;
        }
    };

    /**
     * 操作符文本.
     */
    private final String text;

    /**
     * 创建一个给定文本的数学运算符.
     *
     * @param text 运算符号
     */
    public AbstractMathOperation(final String text) {
        this.text = text;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getText() {
        return text;
    }

    @Override
    public int hashCode() {
        return text != null ? text.hashCode() : 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return text;
    }
}
